﻿using System;
using System.Runtime.InteropServices;
using m_pricelist._BAL_;
using m_pricelist.Proposal_Services;
using Microsoft.Office.Interop.Word;
using Microsoft.VisualBasic;
using Excel = Microsoft.Office.Interop.Excel;
using Word = Microsoft.Office.Interop.Word;

namespace m_pricelist.Services
{
    class PricelistProposal
    {
        #region Member variables
        
        ProposalService ps = new ProposalService();
        bNewProposal bNew;
        Type_Installation m_tInstalParam = new Type_Installation();

        Word.Document wdDoc = null;
        Word.Documents wordDocs = null;
        Word.Document wdDocTerms = null;
        Word.Table tbl = null;

        Word.Application wdApp = null;

        int lRow { get; set; }
        int lwtRow { get; set; }
        int lPSRow { get; set; }

        //Einhverjar global int út VB - Module 1
        int cIsub { get; set; }
        int cItotal { get; set; }
        int cInumber { get; set; }
        int cIdiscount { get; set; }
        int cIunits { get; set; }
        int cIname { get; set; }

        Word.Range PRange = null;
        Word.Range rTermsPRiceList = null;

        double dDiscount { get; set; }
        double dSubTotal { get; set; }
        double dTotal { get; set; }

        //Project
        string sInnovaSign { get; set; }

        string specifier { get; set; }
        string prosenta { get; set; }

        object ReadOnly = true;
        object missing = System.Reflection.Missing.Value;

        //Til að fá dagsetninguna rétta í Date 1 Bookmarkið -
        string date { get; set; }

        string m_sCurrency { get; set; }
        string m_sFinaldest { get; set; }
        string m_sFirstPort { get; set; }

        bool m_bInnova { get; set; }
        bool m_bInstallation { get; set; }

        #endregion

        #region Constructor
        public PricelistProposal()
        {

            lRow = 0;
            lwtRow = 0;
            lPSRow = 0;

            cIsub = 9;
            cItotal = 7;
            cInumber = 3;
            cIdiscount = 8;
            cIunits = 6;
            cIname = 4;

            dDiscount = 0;
            dSubTotal = 0;
            dTotal = 0;

            sInnovaSign = null;

            specifier = "###,##0";
            prosenta = "0.0%";

            date = DateTime.Now.ToShortDateString();
        }
        #endregion

        #region PricelistProposal Gamli
        public void GetPricelistProposal(object sPath, object sFileToOpen, Proposal proposal, NewProposal_BAL newProposal, ref Excel.Worksheet xlSheet)
        {
            try
            {
                string Default_Sheet_Name = "Sheet1";
                if (proposal.xlSheet.Name.Equals(Default_Sheet_Name))
                {
                    GC.Collect();
                    GC.WaitForPendingFinalizers();
                    GC.Collect();
                    GC.WaitForPendingFinalizers();

                    Marshal.FinalReleaseComObject(proposal.xlWorkBookObjects);
                    Marshal.FinalReleaseComObject(proposal.xlSheet);

                    Exception ex = null;
                    throw (ex);

                }
            }
            catch (Exception ex)
            {
                ps.CleanUp(string.Empty);
                System.Windows.Forms.MessageBox.Show("New proposal can not be open since there is no data to work with. Please open Marel Pricelist.xlsm or Project.xlsm and try again");
            }

            try
            {
                m_sFinaldest = proposal.txtFinalAdress_TextChanged();
                if (wdApp == null)
                {
                   wdApp = new Word.Application();
                }
                else
                {
                    wdApp = (Word.Application)System.Runtime.InteropServices.Marshal.GetActiveObject("Word.Application");
                }

                object sInfoPath = Interaction.GetSetting(AppName: "Marel_Consulting", Section: "MFS_Pricelist", Key: "Proposal_Info");

                if (wdDocTerms == null)
                {
                    wdDocTerms = wdApp.Documents.Open(sInfoPath, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);
                }
                else
                {
                    wordDocs.Add(sPath, ref missing, ref missing, ref missing);
                }

                if (proposal.optStandard.Checked == true)
                {
                    if (wdDocTerms.Bookmarks.Exists("Standard"))
                    {
                        rTermsPRiceList = wdDocTerms.Bookmarks.get_Item("Standard").Range;
                    }

                }
                else if (proposal.optLC.Checked == true)
                {
                    if (wdDocTerms.Bookmarks.Exists("LC"))
                    {
                        rTermsPRiceList = wdDocTerms.Bookmarks.get_Item("LC").Range;
                    }
                }
                else if (proposal.optLC100.Checked == true)
                {
                    if (wdDocTerms.Bookmarks.Exists("LC100"))
                    {
                        rTermsPRiceList = wdDocTerms.Bookmarks.get_Item("LC100").Range;
                    }
                }
                else if (proposal.optPrepaid.Checked == true)
                {
                    if (wdDocTerms.Bookmarks.Exists("Prepaid"))
                    {
                        rTermsPRiceList = wdDocTerms.Bookmarks.get_Item("Prepaid").Range;
                    }
                }
                else if (proposal.optOption1.Checked == true)
                {
                    if (wdDocTerms.Bookmarks.Exists("Option1"))
                    {
                        rTermsPRiceList = wdDocTerms.Bookmarks.get_Item("Option1").Range;
                    }
                }
                else if (proposal.optOption2.Checked == true)
                {
                    if (wdDocTerms.Bookmarks.Exists("Option2"))
                    {
                        rTermsPRiceList = wdDocTerms.Bookmarks.get_Item("Option2").Range;
                    }
                }

                // Kallar í GetLastUsedCell fallið, þetta fall verður að skila réttum niðurstöðum.
                try
                {
                    if (proposal.b_pricelist == true)
                        lPSRow = ps.GetLastUsedCell(3, "Output", ref xlSheet);
                    if (proposal.b_project == true)
                        lPSRow = ps.GetLastUsedCell(3, "PriceSummary", ref xlSheet);
                }
                catch (Exception ex)
                {
                    Marshal.FinalReleaseComObject(wdApp);
                    Marshal.FinalReleaseComObject(wdDocTerms);
                    Marshal.FinalReleaseComObject(wdDoc);
                    ps.CleanUp(string.Empty);
                    System.Windows.Forms.MessageBox.Show(ex.ToString() + "There is no data available in the template to work with. ", "@\n" + ex);
                }

                //cmdPro notar þennan parametra
                if (sFileToOpen == "")
                {
                    if (proposal.optTwoLang.Checked == true)
                    {
                        //Opnar Word - "Proposal_English"
                        if (wdDoc == null)
                        {
                            wdDoc = proposal.wdApp.Documents.Open(sPath, ref missing, ref missing, ref missing, 
                                ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, 
                                ref missing, ref missing, ref missing, ref missing, ref missing);
                        }
                        else
                        {
                            wordDocs.Add(sPath, ref missing, ref missing, ref missing);
                        }
                    }
                    else if (proposal.optOneLang.Checked == true)
                    {
                        //Opnar Word - "Proposal_Bilingual"
                        if (wdDoc == null)
                        {
                            wdDoc = proposal.wdApp.Documents.Open(sPath, ref missing, ref missing, ref missing, 
                                ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, 
                                ref missing, ref missing, ref missing, ref missing, ref missing);
                        }
                        else
                        {
                            wordDocs.Add(sPath, ref missing, ref missing, ref missing);
                        }
                    }
                }
                else
                {
                    //Annars opnar það eitthvað template sem notandinn hefur sjálfur valið.
                    wdDoc = proposal.wdApp.Documents.Open(sFileToOpen);
                }

                //Öllum bookmarks og range úr Excel er breytt í streng til að geta tekið tómar sellur líka
                //Ath: nota Convert.ToString(...Excel.sheet....range...);
                if (proposal.b_pricelist == true)
                {
                    if (wdDoc.Bookmarks.Exists("CuName"))
                    {
                        PRange = wdDoc.Bookmarks.get_Item("CuName").Range;
                        PRange.Text = Convert.ToString(proposal.xlWorkBook.Sheets.get_Item("Output").Range("_AccName").Value);
                        wdDoc.Bookmarks.Add("CuName", PRange);
                    }

                    if (wdDoc.Bookmarks.Exists("CuAddress"))
                    {
                        PRange = wdDoc.Bookmarks.get_Item("CuAddress").Range;
                        PRange.Text = Convert.ToString(proposal.xlWorkBook.Sheets["Output"].Range("_BillingAddress").Value);
                        wdDoc.Bookmarks.Add("CuAddress", PRange);
                    }

                    if (wdDoc.Bookmarks.Exists("Date1"))
                    {
                        PRange = wdDoc.Bookmarks.get_Item("Date1").Range;
                        PRange.Text = date;
                        wdDoc.Bookmarks.Add("Date1", PRange);
                    }

                    if (wdDoc.Bookmarks.Exists("Id1"))
                    {
                        PRange = wdDoc.Bookmarks.get_Item("Id1").Range;
                        PRange.Text = Convert.ToString(proposal.xlWorkBook.Sheets["Output"].Range("_ref").Value);
                        wdDoc.Bookmarks.Add("Id1", PRange);
                    }

                    if (wdDoc.Bookmarks.Exists("Layout1"))
                    {
                        PRange = wdDoc.Bookmarks.get_Item("Layout1").Range;
                        PRange.Text = Convert.ToString(proposal.xlWorkBook.Sheets["Output"].Range("_Layout").Value);
                        wdDoc.Bookmarks.Add("Layout1", PRange);
                    }

                    if (wdDoc.Bookmarks.Exists("Layout2"))
                    {
                        PRange = wdDoc.Bookmarks.get_Item("Layout2").Range;
                        PRange.Text = Convert.ToString("Layout: " + proposal.xlWorkBook.Sheets["Output"].Range("_Layout").Value);
                        wdDoc.Bookmarks.Add("Layout2", PRange);
                    }

                    if (wdDoc.Bookmarks.Exists("CuName2"))
                    {
                        PRange = wdDoc.Bookmarks.get_Item("CuName2").Range;
                        PRange.Text = Convert.ToString(proposal.xlWorkBook.Sheets["Output"].Range("_AccName").Value);
                        wdDoc.Bookmarks.Add("CuName2", PRange);
                    }
                }
                if (proposal.b_project == true)
                {
                    if (wdDoc.Bookmarks.Exists("CuName"))
                    {
                        PRange = wdDoc.Bookmarks["CuName"].Range;
                        PRange.Text = proposal.xlWorkBook.Sheets["Main Overview"].Range("_AccName").Value;
                        wdDoc.Bookmarks.Add("CuName", PRange);
                    }
                    if (wdDoc.Bookmarks.Exists("CuAddress"))
                    {
                        PRange = wdDoc.Bookmarks.get_Item("CuAddress").Range;
                        PRange.Text = proposal.xlWorkBook.Sheets["Main Overview"].Range("_BillingAddress").Value;
                        wdDoc.Bookmarks.Add("CuAddress", PRange);
                    }

                    if (wdDoc.Bookmarks.Exists("Date1"))
                    {
                        PRange = wdDoc.Bookmarks.get_Item("Date1").Range;
                        PRange.Text = date;
                        wdDoc.Bookmarks.Add("Date1", PRange);
                    }

                    if (wdDoc.Bookmarks.Exists("Id1"))
                    {
                        PRange = wdDoc.Bookmarks.get_Item("Id1").Range;
                        PRange.Text = Convert.ToString(proposal.xlWorkBook.Sheets["Main Overview"].Range("_ref").Value);
                        wdDoc.Bookmarks.Add("Id1", PRange);
                    }

                    if (wdDoc.Bookmarks.Exists("Layout1"))
                    {
                        PRange = wdDoc.Bookmarks.get_Item("Layout1").Range;
                        PRange.Text = Convert.ToString(proposal.xlWorkBook.Sheets["Main Overview"].Range("_Layout").Value);
                        wdDoc.Bookmarks.Add("Layout1", PRange);
                    }

                    if (wdDoc.Bookmarks.Exists("Layout2"))
                    {
                        PRange = wdDoc.Bookmarks.get_Item("Layout2").Range;
                        PRange.Text = Convert.ToString("Layout: " + proposal.xlWorkBook.Sheets["Main Overview"].Range("_Layout").Value);
                        wdDoc.Bookmarks.Add("Layout2", PRange);
                    }

                    if (wdDoc.Bookmarks.Exists("CuName2"))
                    {
                        PRange = wdDoc.Bookmarks.get_Item("CuName2").Range;
                        PRange.Text = Convert.ToString(proposal.xlWorkBook.Sheets["Main Overview"].Range("_AccName").Value);
                        wdDoc.Bookmarks.Add("CuName2", PRange);
                    }
                }

                // Breytir tölum í double.
                if (proposal.b_pricelist == true)
                {
                    if (lPSRow > 9)
                    {
                        dSubTotal = Convert.ToDouble(proposal.xlWorkBook.Sheets["Output"].Cells(lPSRow, cIsub).Value);
                        dTotal = Convert.ToDouble(proposal.xlWorkBook.Sheets["Output"].Cells(lPSRow, cItotal).Value);
                    }
                }

                if (proposal.b_project == true)
                {
                    dSubTotal = Convert.ToDouble(proposal.xlWorkBook.Sheets["PriceSummary"].Cells(lPSRow, 5).Value);
                    dTotal = Convert.ToDouble(proposal.xlWorkBook.Sheets["PriceSummary"].Cells(lPSRow, 3).Value);
                }

                if (wdDoc.Bookmarks.Exists("Price2"))
                {   //Breytir í streng
                    PRange = wdDoc.Bookmarks.get_Item("Price2").Range;
                    PRange.Text = m_sCurrency + " " + Convert.ToString(Strings.Format(dSubTotal, specifier)) + ", ";

                    if (proposal.optFob.Checked == true)
                    {
                        PRange.Text = PRange.Text + "FOB " + (string.IsNullOrEmpty(m_sFinaldest) ? m_sFirstPort : m_sFinaldest);
                        //PRange.Text = PRange.Text + "FOB " + ((m_sFinaldest == "") ? m_sFirstPort : m_sFinaldest);
                    }
                    else if (proposal.optCFR.Checked == true)
                    {
                        PRange.Text = PRange.Text + "CFR " + (string.IsNullOrEmpty(m_sFinaldest) ? m_sFirstPort : m_sFinaldest);
                        //PRange.Text = PRange.Text + "CFR " + ((m_sFinaldest == "") ? m_sFirstPort : m_sFinaldest);
                    }
                    else if (proposal.optCIF.Checked == true)
                    {
                        PRange.Text = PRange.Text + "CIF " + (string.IsNullOrEmpty(m_sFinaldest) ? m_sFirstPort : m_sFinaldest);
                        //PRange.Text = PRange.Text + "CIF " + ((m_sFinaldest == "") ? m_sFirstPort : m_sFinaldest
                    }
                    else if (proposal.optFCA.Checked == true)
                    {
                        PRange.Text = PRange.Text + "FCA " + (string.IsNullOrEmpty(m_sFinaldest) ? m_sFirstPort : m_sFinaldest);
                        //PRange.Text = PRange.Text + "FCA " + ((m_sFinaldest == "") ? m_sFirstPort : m_sFinaldest);
                    }
                    else if (proposal.optCPT.Checked == true)
                    {
                        PRange.Text = PRange.Text + "CPT " + m_sFinaldest;
                    }
                    else if (proposal.optCIP.Checked == true)
                    {
                        PRange.Text = PRange.Text + "CIP " + m_sFinaldest;
                    }
                    else if (proposal.optDAP.Checked == true)
                    {
                        PRange.Text = PRange.Text + "DAP " + m_sFinaldest;
                    }
                    else if (proposal.optDAT.Checked == true)
                    {
                        PRange.Text = PRange.Text + "DAT " + m_sFinaldest;
                    }
                    wdDoc.Bookmarks.Add("Price2", PRange);
                } // Hér endar (wdDoc.Bookmarks.Exists("Price2")) if setningin

                if (wdDoc.Bookmarks.Exists("Currency"))
                {
                    switch (m_sCurrency)
                    {
                        case "USD":
                            PRange = wdDoc.Bookmarks.get_Item("Currency").Range;
                            PRange.Text = "USD($)";
                            wdDoc.Bookmarks.Add("Currency", PRange);
                            break;
                        case "EUR":
                            PRange = wdDoc.Bookmarks.get_Item("Currency").Range;
                            PRange.Text = "Euros(€)";
                            wdDoc.Bookmarks.Add("Currency", PRange);
                            break;
                        case "GBP":
                            PRange = wdDoc.Bookmarks.get_Item("Currency").Range;
                            PRange.Text = "GBP(£)";
                            wdDoc.Bookmarks.Add("Currency", PRange);
                            break;
                    }
                }

                if (wdDoc.Bookmarks.Exists("Payment"))
                {
                    PRange = wdDoc.Bookmarks.get_Item("Payment").Range;
                    if (rTermsPRiceList != null)
                    {
                        PRange.FormattedText = rTermsPRiceList.FormattedText;
                    }
                    wdDoc.Bookmarks.Add("Payment", PRange);
                }

                if (wdDoc.Bookmarks.Exists("InnovaMaintenance"))
                {
                    if (m_bInnova == true)
                    {
                        rTermsPRiceList = wdDocTerms.Bookmarks.get_Item("InnovaMain").Range;
                    }
                    else
                    {
                        rTermsPRiceList.Text = " ";
                    }

                    PRange = wdDoc.Bookmarks.get_Item("InnovaMaintenance").Range;
                    PRange.FormattedText = rTermsPRiceList.FormattedText;
                    wdDoc.Bookmarks.Add("InnovaMaintenance", PRange);
                }

                if (wdDoc.Bookmarks.Exists("InnovaNote"))
                {
                    if (m_bInnova)
                    {
                        rTermsPRiceList = wdDocTerms.Bookmarks.get_Item("Innova1").Range;
                    }
                    else
                    {
                        rTermsPRiceList.Text = "";
                    }

                    PRange = wdDoc.Bookmarks.get_Item("InnovaNote").Range;
                    PRange.FormattedText = rTermsPRiceList.FormattedText;
                    wdDoc.Bookmarks.Add("InnovaNote", PRange);
                }

                //Prufa
                if (proposal.b_pricelist == true)
                {
                    if (m_bInnova)
                    {

                        Word.Range nyttRange;

                        if (proposal.optOneLang.Checked)
                        {
                            nyttRange = wdDocTerms.Bookmarks.get_Item("InnovaList").Range;
                        }
                        else
                        {
                            nyttRange = wdDocTerms.Bookmarks.get_Item("InnovaList_b").Range;
                        }

                        tbl = wdDocTerms.Tables.Add(nyttRange, lPSRow - 8, 3);
                        tbl.Rows.Height = 1;
                        tbl.Columns[1].Width = 80;
                        tbl.Columns[2].Width = 230;
                        tbl.Columns[3].Width = 40;

                        tbl.Rows[1].Range.Font.Bold = 1;
                        tbl.Rows[1].Range.Borders[Word.WdBorderType.wdBorderBottom].LineStyle = Word.WdLineStyle.wdLineStyleSingle;
                        tbl.Cell(1, 1).Range.Text = "Item number";
                        tbl.Cell(1, 2).Range.Text = "Item Name";
                        tbl.Cell(1, 3).Range.Text = "Units";
                        tbl.Rows[1].Cells.VerticalAlignment = WdCellVerticalAlignment.wdCellAlignVerticalBottom;

                        lwtRow = 2;

                        for (lRow = 10; lRow <= lPSRow; lRow++)
                        {

                            if (proposal.xlWorkBook.Sheets["Output"].Cells(lRow, 30).Value == "sw")
                            {
                                tbl.Cell(lwtRow, 1).Range.Text = Convert.ToString(proposal.xlWorkBook.Sheets["Output"].Cells(lRow, cInumber).Value);
                                tbl.Cell(lwtRow, 2).Range.Text = Convert.ToString(proposal.xlWorkBook.Sheets["Output"].Cells(lRow, cIname).Value);
                                tbl.Cell(lwtRow, 3).Range.Text = Convert.ToString(proposal.xlWorkBook.Sheets["Output"].Cells(lRow, cIunits).Value);
                            }
                            else
                            {
                                tbl.Rows[lwtRow].Delete();
                                lwtRow = lwtRow - 1;
                            }

                            lwtRow = lwtRow + 1;
                        }
                    }
                }

                if (wdDoc.Bookmarks.Exists("InnovaContract"))
                {
                    if (m_bInnova)
                    {
                        if (proposal.optOneLang.Checked)
                        {
                            rTermsPRiceList = wdDocTerms.Bookmarks.get_Item("Innova9s").Range;
                        }
                        else
                        {
                            rTermsPRiceList = wdDocTerms.Bookmarks.get_Item("Innova9b").Range;
                        }
                    }
                    else
                    {
                        rTermsPRiceList.Text = "";
                    }

                    PRange = wdDoc.Bookmarks.get_Item("InnovaContract").Range;
                    PRange.FormattedText = rTermsPRiceList.FormattedText;
                    wdDoc.Bookmarks.Add("InnovaContract", PRange);

                }

                if (wdDoc.Bookmarks.Exists("InnovaSignature"))
                {
                    if (m_bInnova)
                    {
                        if (proposal.b_pricelist == true)
                        {
                            if (wdDoc.Bookmarks.get_Item("InnovaSignature").Range.Text == "[Macro input: Innova Signature – If Innova.]")
                            {
                                rTermsPRiceList = wdDocTerms.Bookmarks.get_Item("InnovaSign").Range;
                                PRange = wdDoc.Bookmarks.get_Item("InnovaSignature").Range;
                                PRange.FormattedText = rTermsPRiceList.FormattedText;
                                wdDoc.Bookmarks.Add("InnovaSignature", PRange);
                            }
                            PRange = wdDoc.Bookmarks.get_Item("CuName4").Range;
                            PRange.Text = Convert.ToString(proposal.xlWorkBook.Sheets["Output"].Range("_AccName").Value);
                            wdDoc.Bookmarks.Add("CuName4", PRange);

                            PRange = wdDoc.Bookmarks.get_Item("CuAddress4").Range;
                            PRange.Text = Convert.ToString(proposal.xlWorkBook.Sheets["Output"].Range("_BillingAddress").Value);
                            wdDoc.Bookmarks.Add("CuAddress4", PRange);

                            PRange = wdDoc.Bookmarks.get_Item("Lastnr").Range;
                            PRange.Text = "10";
                            wdDoc.Bookmarks.Add("Lastnr", PRange);
                        }
                        if (proposal.b_project == true)
                        {
                            sInnovaSign = wdDoc.Bookmarks["InnovaSignature"].Range.Text;
                            if (sInnovaSign == "[Macro input: Innova Signature – If Innova.]" || sInnovaSign == "")
                            {
                                rTermsPRiceList = wdDocTerms.Bookmarks.get_Item("InnovaSign").Range;
                                PRange = wdDoc.Bookmarks.get_Item("InnovaSignature").Range;
                                PRange.FormattedText = rTermsPRiceList.FormattedText;
                                wdDoc.Bookmarks.Add("InnovaSignature", PRange);
                            }

                            PRange = wdDoc.Bookmarks.get_Item("CuName4").Range;
                            PRange.Text = proposal.xlWorkBook.Sheets["Main Overview"].Range("_AccName").Value;
                            wdDoc.Bookmarks.Add("CuName4", PRange);

                            PRange = wdDoc.Bookmarks.get_Item("CuAddress4").Range;
                            PRange.Text = proposal.xlWorkBook.Sheets["Main Overview"].Range("_BillingAddress").Value;
                            wdDoc.Bookmarks.Add("CuAddress4", PRange);

                            PRange = wdDoc.Bookmarks.get_Item("Lastnr").Range;
                            PRange.Text = "10";
                            wdDoc.Bookmarks.Add("Lastnr", PRange);

                            rTermsPRiceList = wdDocTerms.Bookmarks.get_Item("App4").Range;
                        }
                        else
                        {
                            PRange = wdDoc.Bookmarks.get_Item("Lastnr").Range;
                            PRange.Text = "9";
                            wdDoc.Bookmarks.Add("Lastnr", PRange);
                            rTermsPRiceList.Text = "";

                            PRange = wdDoc.Bookmarks.get_Item("InnovaSignature").Range;
                            PRange.FormattedText = rTermsPRiceList.FormattedText;
                            wdDoc.Bookmarks.Add("InnovaSignature", PRange);
                        }
                    }
                }
                else
                {
                    PRange = wdDoc.Bookmarks.get_Item("Lastnr").Range;
                    PRange.Text = "9";
                    wdDoc.Bookmarks.Add("Lastnr", PRange);

                    rTermsPRiceList.Text = "";

                    PRange = wdDoc.Bookmarks.get_Item("InnovaSignature").Range;
                    PRange.FormattedText = rTermsPRiceList.FormattedText;
                    wdDoc.Bookmarks.Add("InnovaSignature", PRange);
                }


                if (proposal.b_project == true)
                {
                    if (wdDoc.Bookmarks.Exists("InnovaApp"))
                    {
                        PRange = wdDoc.Bookmarks.get_Item("InnovaApp").Range;
                        PRange.FormattedText = rTermsPRiceList.FormattedText;
                        wdDoc.Bookmarks.Add("InnovaApp", PRange);
                    }
                }

                wdDocTerms.Close(Type.Missing, Type.Missing, Type.Missing);
                rTermsPRiceList = null;

                //Hér er  instalParam notaður
                if (wdDoc.Bookmarks.Exists("Installation"))
                {
                    if (m_bInstallation)
                    {
                        PRange = wdDoc.Bookmarks.get_Item("Installation").Range;
                        PRange.Text = "included (according to following table)";
                        wdDoc.Bookmarks.Add("Installation", PRange);

                        PRange = wdDoc.Bookmarks.get_Item("HWInstall").Range;
                        PRange.Text = (proposal.chkTHardware.Checked ? Convert.ToString(m_tInstalParam.dHTrainingDays) : "0");
                        wdDoc.Bookmarks.Add("HWInstall", PRange);

                        PRange = wdDoc.Bookmarks.get_Item("HWTraining").Range;
                        PRange.Text = (proposal.chkTHardware.Checked ? Convert.ToString(m_tInstalParam.dHTrainingDays) : "0");
                        wdDoc.Bookmarks.Add("HWTraining", PRange);

                        PRange = wdDoc.Bookmarks.get_Item("SWInstall").Range;
                        PRange.Text = (proposal.chkSoftware.Checked ? Convert.ToString(m_tInstalParam.dSInstalDays) : "0");
                        wdDoc.Bookmarks.Add("SWInstall", PRange);

                        PRange = wdDoc.Bookmarks.get_Item("SWTraining").Range;
                        PRange.Text = (proposal.chkTSoftware.Checked ? Convert.ToString(m_tInstalParam.dSTrainingDays) : "0");
                        wdDoc.Bookmarks.Add("SWTraining", PRange);
                    }

                    else
                    {
                        PRange = wdDoc.Bookmarks.get_Item("Installation").Range;
                        PRange.Text = "not included";
                        wdDoc.Bookmarks.Add("Installation", PRange);
                        PRange = wdDoc.Bookmarks.get_Item("HWInstall").Range;
                        PRange.Text = "0";
                        wdDoc.Bookmarks.Add("HWInstall", PRange);
                        PRange = wdDoc.Bookmarks.get_Item("HWTraining").Range;
                        PRange.Text = "0";
                        wdDoc.Bookmarks.Add("HWTraining", PRange);
                        PRange = wdDoc.Bookmarks.get_Item("SWInstall").Range;
                        PRange.Text = "0";
                        wdDoc.Bookmarks.Add("SWInstall", PRange);
                        PRange = wdDoc.Bookmarks.get_Item("SWTraining").Range;
                        PRange.Text = "0";
                        wdDoc.Bookmarks.Add("SWTraining", PRange);
                    }
                }

                if (wdDoc.Bookmarks.Exists("Delivery"))
                {
                    PRange = wdDoc.Bookmarks.get_Item("Delivery").Range;
                    if (proposal.optFob.Checked)
                    {
                        PRange.Text = "FOB " + (string.IsNullOrEmpty(m_sFinaldest) ? m_sFirstPort : m_sFinaldest);
                        //PRange.Text = "FOB " + ((m_sFinaldest == "") ? m_sFirstPort : m_sFinaldest);
                    }
                    else if (proposal.optCFR.Checked)
                    {
                        PRange.Text = "CFR " + (string.IsNullOrEmpty(m_sFinaldest) ? m_sFirstPort : m_sFinaldest);
                        //PRange.Text = "CFR " + ((m_sFinaldest == "") ? m_sFirstPort : m_sFinaldest);
                    }
                    else if (proposal.optCIF.Checked)
                    {
                        PRange.Text = "CIF " + (string.IsNullOrEmpty(m_sFinaldest) ? m_sFirstPort : m_sFinaldest);
                        //PRange.Text = "CIF " + ((m_sFinaldest == "") ? m_sFirstPort : m_sFinaldest);
                    }
                    else if (proposal.optFCA.Checked)
                    {
                        PRange.Text = "FCA " + (string.IsNullOrEmpty(m_sFinaldest) ? m_sFirstPort : m_sFinaldest);
                        //	PRange.Text = "FCA " + ((m_sFinaldest == "") ? m_sFirstPort : m_sFinaldest);
                    }
                    else if (proposal.optCPT.Checked)
                    {
                        PRange.Text = "CPT " + m_sFinaldest;
                        //PRange.Text = "CPT " + m_sFinaldest;
                    }
                    else if (proposal.optCIP.Checked)
                    {
                        PRange.Text = "CIP " + m_sFinaldest;
                        //	PRange.Text = "CIP " + m_sFinaldest;
                    }
                    else if (proposal.optDAP.Checked)
                    {
                        PRange.Text = "DAP " + m_sFinaldest;
                        //	PRange.Text = "DAP " + m_sFinaldest;
                    }
                    else if (proposal.optDAT.Checked)
                    {
                        PRange.Text = "DAT " + m_sFinaldest;
                        //PRange.Text = "DAT " + m_sFinaldest;
                    }
                    wdDoc.Bookmarks.Add("Delivery", PRange);
                }

                if (proposal.b_pricelist == true)
                {
                    if (wdDoc.Bookmarks.Exists("CuName3"))
                    {
                        PRange = wdDoc.Bookmarks.get_Item("CuName3").Range;
                        PRange.Text = Convert.ToString(proposal.xlWorkBook.Sheets["Output"].Range("_AccName").Value);
                        wdDoc.Bookmarks.Add("CuName3", PRange);
                    }

                    if (wdDoc.Bookmarks.Exists("CuAddress3"))
                    {
                        PRange = wdDoc.Bookmarks.get_Item("CuAddress3").Range;
                        PRange.Text = Convert.ToString(proposal.xlWorkBook.Sheets["Output"].Range("_BillingAddress").Value);
                        wdDoc.Bookmarks.Add("CuAddress3", PRange);
                    }
                }

                if (proposal.b_project == true)
                {
                    if (wdDoc.Bookmarks.Exists("CuName3"))
                    {
                        PRange = wdDoc.Bookmarks.get_Item("CuName3").Range;
                        PRange.Text = Convert.ToString(proposal.xlWorkBook.Sheets["Main Overview"].Range("_AccName").Value);
                        wdDoc.Bookmarks.Add("CuName3", PRange);
                    }

                    if (wdDoc.Bookmarks.Exists("CuAddress3"))
                    {
                        PRange = wdDoc.Bookmarks.get_Item("CuAddress3").Range;
                        PRange.Text = Convert.ToString(proposal.xlWorkBook.Sheets["Main Overview"].Range("_BillingAddress").Value);
                        wdDoc.Bookmarks.Add("CuAddress3", PRange);
                    }
                }

                if (dTotal > 0)
                {
                    PRange = wdDoc.Bookmarks.get_Item("Price").Range;

                    //Þessi forlykkja er notuð ef það þarf að uppfæra Proposal formið og þá er öllum töflum eytt út of ný búin til

                    if (proposal.b_pricelist == true)
                    {
                        foreach (Word.Table oTable in wdDoc.Tables)
                        {
                            if (((Strings.InStr(1, oTable.Cell(1, 1).Range.Text, "Item number", CompareMethod.Text) != 0) &
                                (Strings.InStr(1, oTable.Cell(1, 2).Range.Text, "Item Name", CompareMethod.Text) != 0)))
                            {
                                int j = oTable.Rows.Count;

                                for (var l = j; l >= 1; l--)
                                {
                                    oTable.Rows[l].Delete();
                                }
                                break;
                            }
                        }
                    }
                    if (proposal.b_project == true)
                    {
                        foreach (Word.Table oTable in wdDoc.Tables)
                        {
                            if (((Strings.InStr(1, oTable.Cell(1, 1).Range.Text, "Area", CompareMethod.Text) != 0) &
                                (Strings.InStr(1, oTable.Cell(1, 2).Range.Text, "Description", CompareMethod.Text) != 0)))
                            {
                                int j = oTable.Rows.Count;

                                for (var l = j; l >= 1; l--)
                                {
                                    oTable.Rows[l].Delete();
                                }

                                break;
                            }
                        }
                    }

                    if (proposal.b_pricelist == true)
                    {
                        dDiscount = 1 - (dSubTotal / dTotal);

                        if (dDiscount > 0)
                        {
                            tbl = wdDoc.Tables.Add(PRange, (lPSRow - 8), 6);
                            tbl.Rows.Height = 1; // var 0.5
                            tbl.Columns[1].Width = 80;
                            tbl.Columns[2].Width = 230;
                            tbl.Columns[3].Width = 35;
                            tbl.Columns[4].Width = 60;
                            tbl.Columns[5].Width = 55;
                            tbl.Columns[6].Width = 60;

                            tbl.Rows[1].Range.Font.Bold = 1;
                            tbl.Rows[1].Range.Borders.InsideLineStyle = Word.WdLineStyle.wdLineStyleSingle;
                            tbl.Cell(1, 1).Range.Text = "Item number";
                            tbl.Cell(1, 2).Range.Text = "Item Name";
                            tbl.Cell(1, 3).Range.Text = "Units";
                            tbl.Cell(1, 4).Range.Text = "Price(" + m_sCurrency + ")";
                            tbl.Cell(1, 5).Range.Text = "Discount";
                            tbl.Cell(1, 6).Range.Text = "Contract Price(" + m_sCurrency + ")";
                            tbl.Cell(1, 5).Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;
                            tbl.Rows[1].Cells.VerticalAlignment = WdCellVerticalAlignment.wdCellAlignVerticalBottom;
                        }
                        else
                        {
                            tbl = wdDoc.Tables.Add(PRange, lPSRow - 8, 4);
                            tbl.Rows.Height = 1;//Var 0.5
                            tbl.Columns[1].Width = 100;
                            tbl.Columns[2].Width = 270;
                            tbl.Columns[3].Width = 40;
                            tbl.Columns[4].Width = 70;

                            tbl.Rows[1].Range.Font.Bold = 1;
                            tbl.Rows[1].Range.Borders[Word.WdBorderType.wdBorderBottom].LineStyle = Word.WdLineStyle.wdLineStyleSingle;
                            tbl.Cell(1, 1).Range.Text = "Item number";
                            tbl.Cell(1, 2).Range.Text = "Item Name";
                            tbl.Cell(1, 3).Range.Text = "Units";
                            tbl.Cell(1, 4).Range.Text = "Price(" + m_sCurrency + ")";
                            tbl.Cell(1, 4).Range.Text = "Contract Price(" + m_sCurrency + ")";
                            tbl.Cell(1, 4).Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;
                            tbl.Rows[1].Cells.VerticalAlignment = WdCellVerticalAlignment.wdCellAlignVerticalBottom;
                        }

                        lwtRow = 2;
                        try
                        {
                            //For lykkja
                            for (lRow = 10; lRow <= lPSRow; lRow++)
                            {
                                if (!proposal.xlWorkBook.Sheets["Output"].Rows[lRow].Hidden)
                                {
                                    if (Strings.InStr(1, Convert.ToString(proposal.xlWorkBook.Sheets["Output"].Cells(lRow, cInumber).Value), "Total", CompareMethod.Text) != 0)
                                    {
                                        tbl.Cell(lwtRow, 2).Range.Text = Convert.ToString(proposal.xlWorkBook.Sheets["Output"].Cells(lRow, cInumber).Value);
                                        tbl.Cell(lwtRow, 4).Range.Text = Convert.ToString(proposal.xlWorkBook.Sheets["Output"].Cells(lRow, cItotal).Value.ToString(specifier));
                                        tbl.Cell(lwtRow, 4).Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphRight;

                                        if (dDiscount > 0)
                                        {
                                            tbl.Cell(lwtRow, 5).Range.Text = Convert.ToString(proposal.xlWorkBook.Sheets["Output"].Cells(lRow, cIdiscount).Value.ToString(prosenta));
                                            tbl.Cell(lwtRow, 6).Range.Text = Convert.ToString(proposal.xlWorkBook.Sheets["Output"].Cells(lRow, cIsub).Value.ToString(specifier));
                                            tbl.Cell(lwtRow, 5).Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;
                                            tbl.Cell(lwtRow, 6).Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphRight;
                                        }
                                        tbl.Rows[lwtRow].Range.Borders[Word.WdBorderType.wdBorderTop].LineStyle = Word.WdLineStyle.wdLineStyleSingle;
                                        tbl.Rows[lwtRow].Range.Borders[Word.WdBorderType.wdBorderBottom].LineStyle = Word.WdLineStyle.wdLineStyleDouble;
                                        tbl.Rows[lwtRow].Range.Font.Bold = 1;
                                    }
                                    else
                                    {
                                        tbl.Cell(lwtRow, 1).Range.Text = Convert.ToString(proposal.xlWorkBook.Sheets["Output"].Cells(lRow, cInumber).Value);
                                        tbl.Cell(lwtRow, 2).Range.Text = Convert.ToString(proposal.xlWorkBook.Sheets["Output"].Cells(lRow, cIname).Value);
                                        tbl.Cell(lwtRow, 3).Range.Text = Convert.ToString(proposal.xlWorkBook.Sheets["Output"].Cells(lRow, cIunits).Value);
                                        if (String.IsNullOrEmpty(Convert.ToString(proposal.xlWorkBook.Sheets["Output"].Cells(lRow, cItotal).Value)))
                                        {
                                            String str2 = Convert.ToString(proposal.xlWorkBook.Sheets["Output"].Cells(lRow, cItotal).Value);
                                            str2 = " ";
                                            tbl.Cell(lwtRow, 4).Range.Text = str2;
                                        }
                                        else
                                        {
                                            tbl.Cell(lwtRow, 4).Range.Text = Convert.ToString(proposal.xlWorkBook.Sheets["Output"].Cells(lRow, cItotal).Value.ToString(specifier));
                                        }
                                        tbl.Cell(lwtRow, 4).Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphRight;

                                        if (dDiscount > 0)
                                        {
                                            tbl.Cell(lwtRow, 5).Range.Text = Convert.ToString(proposal.xlWorkBook.Sheets["Output"].Cells(lRow, cIdiscount).Text);
                                            tbl.Cell(lwtRow, 6).Range.Text = Convert.ToString(proposal.xlWorkBook.Sheets["Output"].Cells(lRow, cIsub).Value.ToString(specifier));
                                            tbl.Cell(lwtRow, 5).Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;
                                            tbl.Cell(lwtRow, 6).Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphRight;
                                        }

                                        //if ((Convert.ToString(xlWorkBook.Sheets["Output"].Cells(lRow, cItotal).Value)) == "" & ((Convert.ToString(xlApp.Sheets["Output"].Cells(lRow, cInumber).Value)) != ""))
                                        //{
                                        //    tbl.Rows[lwtRow].Delete();
                                        //    lwtRow = lwtRow - 1;
                                        //}
                                    }
                                }
                                else
                                {
                                    tbl.Rows[lwtRow].Delete();
                                    lwtRow = lwtRow - 1;
                                }

                                lwtRow = lwtRow + 1;
                            }//Hér endar forlykkjan
                        }
                        catch (Exception ex)
                        {
                            System.Windows.Forms.MessageBox.Show(ex.ToString() + "An error came up ", "@\n" + ex);
                            System.Runtime.InteropServices.Marshal.ReleaseComObject(wdDocTerms);
                            wdDocTerms = null;

                            System.Runtime.InteropServices.Marshal.ReleaseComObject(wdDoc);
                            wdDoc = null;

                            System.Runtime.InteropServices.Marshal.ReleaseComObject(proposal.wdApp);
                            proposal.wdApp = null;
                            ps.CleanUp(string.Empty);
                        }


                    }//Hér endar dTotal if setningin

                    if (proposal.b_project == true)
                    {
                        dDiscount = Convert.ToDouble(proposal.xlWorkBook.Sheets["PriceSummary"].Cells(lPSRow, 4).Value);

                        if (dDiscount > 0)
                        {
                            tbl = wdDoc.Tables.Add(PRange, lPSRow, 5);
                            tbl.Rows.Height = 1; // var 0.5
                            tbl.Columns[1].Width = 50;
                            tbl.Columns[2].Width = 230;
                            tbl.Columns[3].Width = 60;
                            tbl.Columns[4].Width = 60;
                            tbl.Columns[5].Width = 70;

                            tbl.Rows[1].Range.Font.Bold = 1;
                            tbl.Rows[1].Range.Borders[Word.WdBorderType.wdBorderBottom].LineStyle = Word.WdLineStyle.wdLineStyleSingle;
                            tbl.Cell(1, 1).Range.Text = "Area";
                            tbl.Cell(1, 2).Range.Text = "Description";
                            tbl.Cell(1, 3).Range.Text = "Price(" + m_sCurrency + ")";
                            tbl.Cell(1, 4).Range.Text = "Discount";
                            tbl.Cell(1, 5).Range.Text = "Contract Price(" + m_sCurrency + ")";
                            tbl.Cell(1, 5).Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;
                            tbl.Rows[1].Cells.VerticalAlignment = WdCellVerticalAlignment.wdCellAlignVerticalBottom;
                        }
                        else
                        {
                            tbl = wdDoc.Tables.Add(PRange, lPSRow, 3);
                            tbl.Rows.Height = 1;//Var 0.5
                            tbl.Columns[1].Width = 60;
                            tbl.Columns[2].Width = 300;
                            tbl.Columns[3].Width = 90;

                            tbl.Rows[1].Range.Font.Bold = 1;
                            tbl.Rows[1].Range.Borders[Word.WdBorderType.wdBorderBottom].LineStyle = Word.WdLineStyle.wdLineStyleSingle;
                            tbl.Cell(1, 1).Range.Text = "Area";
                            tbl.Cell(1, 2).Range.Text = "Description";
                            tbl.Cell(1, 3).Range.Text = "Contract Price(" + m_sCurrency + ")";
                            tbl.Cell(1, 3).Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;
                            tbl.Rows[1].Cells.VerticalAlignment = WdCellVerticalAlignment.wdCellAlignVerticalBottom;
                        }
                        lwtRow = 2;
                        try
                        {
                            //For lykkja
                            for (lRow = 2; lRow <= lPSRow; lRow++)
                            {
                                if (Strings.InStr(1, Convert.ToString(proposal.xlWorkBook.Sheets["PriceSummary"].Cells(lRow + 3, 1).Value), "Total", CompareMethod.Text) != 0)
                                {
                                    if (Convert.ToString(proposal.xlWorkBook.Sheets["PriceSummary"].Cells(lRow + 3 - 1, 3).Value) == null)
                                    {
                                        tbl.Rows[lwtRow - 1].Delete();
                                        lwtRow = lwtRow - 1;
                                    }

                                    tbl.Cell(lwtRow, 2).Range.Text = Convert.ToString(proposal.xlWorkBook.Sheets["PriceSummary"].Cells(lRow + 3, 1).Value);
                                    tbl.Cell(lwtRow, 3).Range.Text = Convert.ToString(proposal.xlWorkBook.Sheets["PriceSummary"].Cells(lRow + 3, 3).Value.ToString(specifier));
                                    tbl.Cell(lwtRow, 3).Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphRight;

                                    if (dDiscount > 0)
                                    {
                                        tbl.Cell(lwtRow, 4).Range.Text = Convert.ToString(proposal.xlWorkBook.Sheets["PriceSummary"].Cells(lRow + 3, 4).Value.ToString(prosenta));
                                        tbl.Cell(lwtRow, 5).Range.Text = Convert.ToString(proposal.xlWorkBook.Sheets["PriceSummary"].Cells(lRow + 3, 5).Value.ToString(specifier));
                                        tbl.Cell(lwtRow, 4).Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;
                                        tbl.Cell(lwtRow, 5).Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphRight;
                                    }
                                    tbl.Rows[lwtRow].Range.Borders[Word.WdBorderType.wdBorderTop].LineStyle = Word.WdLineStyle.wdLineStyleSingle;
                                    tbl.Rows[lwtRow].Range.Borders[Word.WdBorderType.wdBorderBottom].LineStyle = Word.WdLineStyle.wdLineStyleDouble;
                                    tbl.Rows[lwtRow].Range.Font.Bold = 1;
                                }
                                else
                                {
                                    string str = Convert.ToString(proposal.xlWorkBook.Sheets["PriceSummary"].Cells(lRow + 3, 3).Value);
                                    switch (str)
                                    {
                                        case "0":

                                            tbl.Cell(lwtRow, 1).Range.Text = Convert.ToString(proposal.xlWorkBook.Sheets["PriceSummary"].Cells(lRow + 3, 1).Value);
                                            tbl.Cell(lwtRow, 2).Range.Text = Convert.ToString(proposal.xlWorkBook.Sheets["PriceSummary"].Cells(lRow + 3, 2).Value);
                                            tbl.Cell(lwtRow, 3).Range.Text = Convert.ToString(proposal.xlWorkBook.Sheets["PriceSummary"].Cells(lRow + 3, 3).Value.ToString(specifier));
                                            tbl.Cell(lwtRow, 3).Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphRight;
                                            if (dDiscount > 0)
                                            {
                                                tbl.Cell(lwtRow, 4).Range.Text = Convert.ToString(proposal.xlWorkBook.Sheets["PriceSummary"].Cells(lRow + 3, 4).Text);
                                                tbl.Cell(lwtRow, 5).Range.Text = Convert.ToString(proposal.xlWorkBook.Sheets["PriceSummary"].Cells(lRow + 3, 5).Value.ToString(specifier));
                                                tbl.Cell(lwtRow, 4).Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;
                                                tbl.Cell(lwtRow, 5).Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphRight;
                                            }

                                            break;
                                        default:

                                            tbl.Cell(lwtRow, 1).Range.Text = Convert.ToString(proposal.xlWorkBook.Sheets["PriceSummary"].Cells(lRow + 3, 1).Value);

                                            tbl.Cell(lwtRow, 2).Range.Text = Convert.ToString(proposal.xlWorkBook.Sheets["PriceSummary"].Cells(lRow + 3, 2).Value);
                                            if (String.IsNullOrEmpty(Convert.ToString(proposal.xlWorkBook.Sheets["PriceSummary"].Cells(lRow + 3, 3).Value)))
                                            {
                                                String str2 = Convert.ToString(proposal.xlWorkBook.Sheets["PriceSummary"].Cells(lRow + 3, 3).Value);
                                                str2 = " ";
                                                tbl.Cell(lwtRow, 3).Range.Text = str2;
                                            }
                                            else
                                            {
                                                tbl.Cell(lwtRow, 3).Range.Text = Convert.ToString(proposal.xlWorkBook.Sheets["PriceSummary"].Cells(lRow + 3, 3).Value.ToString(specifier));
                                            }
                                            tbl.Cell(lwtRow, 3).Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphRight;
                                            if (dDiscount > 0)
                                            {
                                                if (String.IsNullOrEmpty(Convert.ToString(proposal.xlWorkBook.Sheets["PriceSummary"].Cells(lRow + 3, 4).Value)))
                                                {
                                                    String str3 = Convert.ToString(proposal.xlWorkBook.Sheets["PriceSummary"].Cells(lRow + 3, 4).Value);
                                                    str3 = " ";
                                                    tbl.Cell(lwtRow, 4).Range.Text = str3;
                                                }
                                                else
                                                {
                                                    tbl.Cell(lwtRow, 4).Range.Text = Convert.ToString(proposal.xlWorkBook.Sheets["PriceSummary"].Cells(lRow + 3, 4).Value.ToString(prosenta));
                                                }

                                                if (String.IsNullOrEmpty(Convert.ToString(proposal.xlWorkBook.Sheets["PriceSummary"].Cells(lRow + 3, 5).Value)))
                                                {
                                                    String str4 = Convert.ToString(proposal.xlWorkBook.Sheets["PriceSummary"].Cells(lRow + 3, 5).Value);
                                                    str4 = " ";
                                                    tbl.Cell(lwtRow, 5).Range.Text = str4;
                                                }
                                                else
                                                {
                                                    tbl.Cell(lwtRow, 5).Range.Text = Convert.ToString(proposal.xlWorkBook.Sheets["PriceSummary"].Cells(lRow + 3, 5).Value.ToString(specifier));
                                                }

                                                tbl.Cell(lwtRow, 4).Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;
                                                tbl.Cell(lwtRow, 5).Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphRight;
                                            }
                                            break;
                                    }
                                }
                                lwtRow = lwtRow + 1;
                            }//Hér endar forlykkjan

                            if (m_bInnova)
                            {

                                lPSRow = ps.GetLastUsedCell(14, "Registration", ref xlSheet);
                                if (proposal.optOneLang.Checked)
                                {
                                    PRange = wdDoc.Bookmarks.get_Item("InnovaList").Range;
                                }
                                else
                                {
                                    PRange = wdDoc.Bookmarks.get_Item("InnovaList_b").Range;
                                }

                                PRange.ParagraphFormat.SpaceAfter = 0;
                                PRange.ParagraphFormat.SpaceBefore = 0;

                                if (PRange != null)
                                {
                                    tbl = wdDoc.Tables.Add(PRange, lPSRow - 2, 3);
                                    tbl.Rows.Height = 1;
                                    tbl.Columns[1].Width = 80;
                                    tbl.Columns[2].Width = 230;
                                    tbl.Columns[3].Width = 40;


                                    tbl.Rows[1].Range.Font.Bold = 1;
                                    tbl.Rows[1].Range.Borders[Word.WdBorderType.wdBorderBottom].LineStyle = Word.WdLineStyle.wdLineStyleSingle;
                                    tbl.Cell(1, 1).Range.Text = "Item number";
                                    tbl.Cell(1, 2).Range.Text = "Item Name";
                                    tbl.Cell(1, 3).Range.Text = "Units";
                                    tbl.Rows[1].Cells.VerticalAlignment = Word.WdCellVerticalAlignment.wdCellAlignVerticalBottom;

                                    lwtRow = 2;


                                    for (int lRow2 = 4; lRow2 <= lPSRow; lRow2++)
                                    {

                                        if (proposal.xlWorkBook.Sheets["Registration"].Cells(lRow2, 14).Value == 6)
                                        {
                                            tbl.Cell(lwtRow, 1).Range.Text = Convert.ToString(proposal.xlWorkBook.Sheets["Registration"].Cells(lRow, 5).Value);
                                            tbl.Cell(lwtRow, 2).Range.Text = Convert.ToString(proposal.xlWorkBook.Sheets["Registration"].Cells(lRow, 6).Value);
                                            tbl.Cell(lwtRow, 3).Range.Text = Convert.ToString(proposal.xlWorkBook.Sheets["Registration"].Cells(lRow2, 3).Value);
                                        }
                                        else
                                        {
                                            tbl.Rows[lwtRow].Delete();
                                            lwtRow = lwtRow - 1;
                                        }

                                        lwtRow = lwtRow + 1;
                                    }
                                }
                            }

                            //test
                            string sShortName = null;
                            string sFileName = null;

                            if (proposal.lsbAppIn.Items.Count > 0)
                            {
                                for (var l = 0; l < proposal.lsbAppIn.Items.Count; l++)
                                {
                                    sShortName = proposal.lsbShortNameList[0].ToString();
                                    sFileName = Strings.Replace(sInfoPath.ToString(), "ProposalInfo.docx", "") + proposal.lsbAppNameList[0].ToString();
                                    switch (l)
                                    {
                                        case 0:
                                            if (wdDoc.Bookmarks.Exists("App1a"))
                                            {
                                                PRange = wdDoc.Bookmarks["App1aName"].Range;
                                                PRange.Text = sShortName;

                                                PRange = wdDoc.Bookmarks["App1a"].Range;
                                                wdDocTerms = proposal.wdApp.Documents.Open(sFileName);

                                                rTermsPRiceList = wdDocTerms.Bookmarks["App1"].Range;
                                                PRange.FormattedText = rTermsPRiceList.FormattedText;
                                                PRange.ParagraphFormat.SpaceAfter = 0;
                                                PRange.ParagraphFormat.SpaceBefore = 0;
                                                wdDoc.Bookmarks.Add("App1a", PRange);
                                                wdDocTerms.Close(Type.Missing, Type.Missing, Type.Missing);
                                                rTermsPRiceList = null;
                                            }
                                            break;

                                        case 1:
                                            if (wdDoc.Bookmarks.Exists("App1b"))
                                            {
                                                PRange = wdDoc.Bookmarks["App1bName"].Range;
                                                PRange.Text = sShortName;

                                                PRange = wdDoc.Bookmarks["App1b"].Range;
                                                wdDocTerms = proposal.wdApp.Documents.Open(sFileName);
                                                rTermsPRiceList = wdDocTerms.Bookmarks["App1"].Range;
                                                PRange.FormattedText = rTermsPRiceList.FormattedText;
                                                PRange.ParagraphFormat.SpaceAfter = 0;
                                                PRange.ParagraphFormat.SpaceBefore = 0;
                                                wdDoc.Bookmarks.Add("App1b", PRange);
                                                wdDocTerms.Close(false);
                                            }

                                            break;
                                    }

                                }
                            }


                            if (wdDoc.Bookmarks.Exists("Appendix3"))
                            {
                                if (!proposal.chkApp3.Checked)
                                {
                                    PRange = wdDoc.Bookmarks["Appendix3"].Range;
                                    PRange.Text = "";
                                    wdDoc.Bookmarks.Add("Appendix3", PRange);
                                }
                                else
                                {
                                    PRange = wdDoc.Bookmarks["Appendix3"].Range;

                                    if (PRange.Text == "")
                                    {
                                        wdDocTerms = proposal.wdApp.Documents.Open(sInfoPath);
                                        rTermsPRiceList = wdDocTerms.Bookmarks["App3"].Range;
                                        PRange.FormattedText = rTermsPRiceList.FormattedText;
                                        PRange.ParagraphFormat.SpaceAfter = 0;
                                        PRange.ParagraphFormat.SpaceBefore = 0;
                                        wdDoc.Bookmarks.Add("Appendix3", PRange);
                                        wdDocTerms.Close(Type.Missing, Type.Missing, Type.Missing);
                                        rTermsPRiceList = null;
                                    }
                                }
                            }
                        }


                        catch (Exception ex)
                        {
                            //Skila til baka minni og objectum
                            GC.Collect();
                            GC.WaitForPendingFinalizers();
                            GC.Collect();
                            GC.WaitForPendingFinalizers();

                            System.Runtime.InteropServices.Marshal.ReleaseComObject(wdDocTerms);
                            wdDocTerms = null;

                            System.Runtime.InteropServices.Marshal.ReleaseComObject(wdDoc);
                            wdDoc = null;

                            System.Runtime.InteropServices.Marshal.ReleaseComObject(proposal.wdApp);
                            proposal.wdApp = null;

                            System.Windows.Forms.MessageBox.Show("An error came up ", "@\n" + ex);
                        }
                    }
                }



                //Til að bæta við footer í skjalið
                foreach (Word.Section wordSection in wdDoc.Sections)
                {
                    wordSection.Footers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range.Fields.Update();
                }

                object Save = false;

                // wdDocTerms.Close(Save, Type.Missing, Type.Missing);

                wdDoc.Activate();
                proposal.wdApp.Visible = true;
                proposal.wdApp.ScreenUpdating = true;

                //Hreinsa upp
                GC.Collect();
                GC.WaitForPendingFinalizers();

                GC.Collect();
                GC.WaitForPendingFinalizers();

                System.Runtime.InteropServices.Marshal.ReleaseComObject(PRange);

                System.Runtime.InteropServices.Marshal.ReleaseComObject(wdDocTerms);
                wdDocTerms = null;

                proposal.cmdClose_Click(this, EventArgs.Empty);

            }//Try endar hér
            catch (Exception ex)
            {
                //CleanUp();
                System.Windows.Forms.MessageBox.Show(ex.ToString() + " An error came up  ", "@\n" + ex);

                //Hreinsa upp
                System.Runtime.InteropServices.Marshal.ReleaseComObject(proposal.wdApp);
                proposal.wdApp = null;
              
                GC.Collect();
                GC.WaitForPendingFinalizers();

                GC.Collect();
                GC.WaitForPendingFinalizers();
            }
        }
        #endregion

        #region UserForm_Initialize_PriceList
        //Þetta fall er notað til að upphafstilla Pricelist formið ef Marel_Pricelist skjalið er valið
        public void UserForm_Initialize_PriceList(Proposal proposal, bool b_pricelist, ref Excel.Worksheet xlSheet)
        {
            try
            {
                bNew = new bNewProposal();
                proposal = new Proposal();
                string m_sPath = "";
                string sDelivery = null;
                string sSolutionType = null;
                Excel.Range rFundin = null;

                m_sCurrency = proposal.xlWorkBook.CustomDocumentProperties("Currency").Value;
                m_sFinaldest = proposal.xlWorkBook.CustomDocumentProperties("FinalDest").Value;
                sDelivery = proposal.xlWorkBook.CustomDocumentProperties("DeliveryTerms").Value;
                sSolutionType = proposal.xlWorkBook.CustomDocumentProperties("SolutionType").Value;
                m_sFirstPort = proposal.xlWorkBook.CustomDocumentProperties("FirstPort").Value;

                //Call chkSeaOnly_Change
                proposal.chkSeaOnly_CheckedChanged(this, EventArgs.Empty);
                if (sSolutionType == "C")
                {
                    proposal.chkApp3_CheckedChanged(this, EventArgs.Empty);
                }

                //If og else setningar sem tengjast Delivery terms Frame

                //fullt af If og else setningum
                if (sDelivery == "FOB")
                {
                    proposal.optFob_CheckedChanged(this, EventArgs.Empty,  m_sFirstPort);
                }
                else if (sDelivery == "CFR")
                {
                    proposal.optCFR_CheckedChanged(this, EventArgs.Empty, m_sFinaldest, m_sFirstPort);
                }
                else if (sDelivery == "CIF")
                {
                    proposal.optCIF_CheckedChanged(this, EventArgs.Empty, m_sFinaldest, m_sFirstPort);
                }
                else if (sDelivery == "FCA")
                {
                    proposal.optFCA_CheckedChanged(this, EventArgs.Empty, m_sFirstPort);
                }
                else if (sDelivery == "CPT")
                {
                    proposal.optCPT_CheckedChanged(this, EventArgs.Empty, m_sFinaldest);
                }
                else if (sDelivery == "CIP")
                {
                  proposal.optCIP_CheckedChanged(this, EventArgs.Empty, m_sFinaldest);
                }
                else if (sDelivery == "DAP")
                {
                    proposal.optDAP_CheckedChanged(this, EventArgs.Empty, m_sFinaldest);
                }
                else if (sDelivery == "DAT")
                {
                    proposal.optDAT_CheckedChanged(this, EventArgs.Empty, m_sFinaldest);
                }

                object What = "Innova";
                object After = xlSheet.Cells[5000, 3];
                object LookIn = Excel.XlFindLookIn.xlValues;
                object LookAt = Excel.XlLookAt.xlPart;
                object SearchOrder = Excel.XlSearchOrder.xlByRows;
                Excel.XlSearchDirection SearchDirection = Excel.XlSearchDirection.xlNext;
                object MatchCase = System.Reflection.Missing.Value;
                object MatchByte = System.Reflection.Missing.Value;
                object SearchFormat = System.Reflection.Missing.Value;

                rFundin = xlSheet.Cells.Find(What, After, LookIn, LookAt, SearchOrder, SearchDirection, 
                    Type.Missing, Type.Missing, Type.Missing);
                if (rFundin == null)
                    m_bInnova = false;
                else
                    m_bInnova = true;

                m_sPath = Interaction.GetSetting(AppName: "Marel_Consulting", Section: "MFS_Pricelist", Key: "Pricelist_Location");

                ps.InstallParameter(m_tInstalParam, proposal);
                if (m_tInstalParam.bIHardware)
                { 
                    proposal.chkHardware_CheckedChanged(this, EventArgs.Empty);
                    m_bInstallation = true; 
                }

                if (m_tInstalParam.bTHardware) 
                { 
                    proposal.chkTHardware_CheckedChanged(this,EventArgs.Empty); 
                    m_bInstallation = true; 
                }
                
                if (m_tInstalParam.bISoftware) 
                { 
                    proposal.chkSoftware_CheckedChanged(this,EventArgs.Empty); 
                    m_bInstallation = true; 
                }

                if (m_tInstalParam.bTSoftware) 
                { 
                    proposal.chkTSoftware_CheckedChanged(this,EventArgs.Empty); 
                    m_bInstallation = true; 
                }

                proposal.optOneLang_CheckedChanged(this, EventArgs.Empty);
                proposal.optStandard_CheckedChanged(this, EventArgs.Empty);
                
                bNew.bNewStyle(sDelivery, proposal); //Kalla á bNewStyle fallið

                proposal.xlWorkBook.Sheets["Output"].Columns["AF"].Hidden = false;

                proposal.xlWorkBook.Sheets["Output"].Columns["AF"].Hidden = true;
            }
            catch (Exception ex)
            {
                System.Windows.Forms.MessageBox.Show("An error came up in the initialization of the form. It will now be closed. Please try to opan again" +
                    " /n" + ex.ToString());
                GC.Collect();
                GC.WaitForPendingFinalizers();
                GC.Collect();
                GC.WaitForPendingFinalizers();
                proposal.cmdClose_Click(this, EventArgs.Empty);
            }
        }
     
        #endregion

    }

}
